'use strict';

stareal
    .controller("LoginController", function ($scope, $api, $stateParams, $alert, $document, localStorageService, $state, $interval,$rootScope) {
        $scope.cdState = true;  //切换状态
        $scope.telphone_no = "";
        $scope.accessToken = "";
        $scope.paracont = "获取验证码";
        $scope.code = "";
        $scope.password = "";
        $scope.password2 = "";
        $scope.paramsList=[];
        $scope.pass = "";
        $scope.isActive = false;
        $scope.sendCodeStatus=false;
        $scope.good_id=$stateParams.good_id;
        $("#mylogin").height(document.documentElement.clientHeight);
        $scope.$watch('code',function (newValue, oldValue) {
           //console.log($scope.code);
            $scope.code=$scope.code;
            if( $scope.code!=''&&$scope.code!=null&&$scope.code!=undefined){
                $scope.isActive =  true;
            }else{
                $scope.isActive =  false;
            }
        });
        //埋点
        $scope.maiDian=function(type,action,label,id){
            if(_czc!=undefined&&_czc!=null&&_czc!='') {
                _czc.push(["_trackEvent", type, action, label, '', id]);
            }
        }
        $scope.$watch('pass',function (newValue, oldValue) {
            // console.log($scope.pass);
            if( $scope.pass!=''){
                $scope.isActive =  true;
            }else{
                $scope.isActive =  false;
            }
        });
        $scope.goBack = function() {
            $rootScope.back()//直接使用
        },
        $scope.login = {
            cd:function () {
                if($scope.cdState){
                    if(_czc!=undefined&&_czc!=null&&_czc!='') {
                        _czc.push(["_trackEvent", 'h5-登录-短信快捷登录', '短信快捷登录', '查看', 'login']);
                    }
                }
                $scope.cdState  = !$scope.cdState;
            },
            sendCode:function (telphone_no) {
                var second = 60;
                var timerHandler = undefined;
                if (!this.validatemobile(telphone_no)) {
                    return;
                }
                if($scope.sendCodeStatus){
                    return;
                }
                // 验证码
                $api.get("app/login/code/retrieve", {mobile:telphone_no, type: "0"})
                    .then(function (ret) {
                        if (ret.retCode == "0") {
                            $alert.show("验证码已发送!");
                            timerHandler = $interval(function () {
                                if (second <= 0) {
                                    $interval.cancel(timerHandler);
                                    timerHandler = undefined;
                                    second = 60;
                                    $scope.paracont = "重发";
                                    $scope.sendCodeStatus=false;
                                } else {
                                    $scope.sendCodeStatus=true;
                                    $scope.paracont = second + "秒";
                                    second--;
                                }
                            }, 1000, 100)
                            localStorageService.set('code_token', ret.accessToken);
                        } else {
                            $alert.show("验证码发送失败，请稍后重试!");
                        }
                    },function(err){
                        $alert.show(err);
                });
            },
            login:function (telphone_no,code,state){
                if(!$scope.isActive){
                    return;
                }
                if (!this.validatemobile(telphone_no)) {
                    return;
                }
                if(state==1){//验证码登录
                    if (!localStorageService.get('code_token')) {
                        $alert.show("请先获取验证码！");
                        return false;
                    }
                    if($("#code").val()==null||$("#code").val()==undefined||$("#code").val()==""||code==''){
                        $alert.show("请输入验证码！");
                        return false;
                    }
                    // console.log(code+'n')
                    var _params = {
                        mobile:telphone_no,
                        code:$("#code").val().trim(),
                        accessToken: localStorageService.get('code_token')
                    };
                }
                if(state==2){//密码登录
                    var _params = {
                        mobile:telphone_no,
                        password:code
                    };
                }
                $api.post("app/login/user/retrieve", _params)
                    .then(function (ret) {
                        localStorageService.set("telphone_no",telphone_no);
                        localStorageService.set("user",ret); //存储用户信息
                        $scope.accessToken = ret.accessToken;
                        localStorageService.set('login_token', ret.accessToken);
                        location.href = "oauth/web?accessToken=" + ret.accessToken + "&state="+encodeURIComponent($stateParams.good_id);
                    }, function (err) {
                        $alert.show(err);
                    });
            },
            validatemobile:function (mobile) {
                if(!mobile){
                    $alert.show('请输入手机号码！');
                    return false
                }
                if (mobile.toString().length == 0) {
                    $alert.show('请输入手机号码！');
                    return false;
                }
                if (mobile.toString().length != 11) {
                    $alert.show('请输入11位手机号码！');
                    return false;
                }
                var myreg = /^1[3|4|5|7|6|8|9][0-9]{9}$/; //验证规则
                if (!myreg.test(mobile)) {
                    $alert.show('请输入有效的手机号码！');
                    return false;
                }
                return true;
            },
            save:function () {
                if(!localStorageService.get("rese_tel")||!localStorageService.get("rese_code")||!localStorageService.get('code_token')){
                    $alert.show("请填写正确的手机号或验证码");
                    return false;
                }
                if($scope.password==null&&$scope.password==''&&$scope.password==undefined){
                    $alert.show("密码不能为空");
                    return false;
                }
                if($scope.password.length<6){
                    $alert.show("请设置大于6位数密码")
                    return false;
                }
                if($scope.password.length>18){
                    $alert.show("请设置小于18位数密码")
                    return false;
                }
                if($scope.password!==$scope.password2){
                    $alert.show("两次输入的密码不一致");
                    return false;
                }
                var regExp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
                if (!regExp.test($scope.password)){
                    $alert.show("请设置6~18位字母和数字组合的密码")
                    return false;
                }
                var _params = {
                    mobile: localStorageService.get("rese_tel"),
                    code: localStorageService.get("rese_code"),
                    password:$scope.password,
                    smsToken: localStorageService.get('code_token')
                };
                $api.post("app/login/user/forget",_params)
                    .then(function (ret) {
                        $alert.show("修改成功");
                        // $state.go('main.login',{good_id:''})
                    },function (err) {
                        $alert.show(err)
                    })
            }
        }
    });